import os
from fastapi import FastAPI
from hooks.lifsepan import  lifespan
from loguru import logger
from hooks.middlewares import log_middleware
from starlette.middleware.base import BaseHTTPMiddleware
from routers import  user, address
from dotenv import load_dotenv
import uvicorn
import settings

load_dotenv()
ALIBABA_CLOUD_ACCESS_KEY_ID = os.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
ALIBABA_CLOUD_ACCESS_KEY_SECRET = os.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
host = os.getenv('REDIS_HOST')
port = os.getenv('REDIS_PORT')
app = FastAPI(lifespan=lifespan)
app.add_middleware(BaseHTTPMiddleware, dispatch=log_middleware)
app.include_router(user.router)
app.include_router(address.router)

@app.get("/")
async def root():
    logger.info("This formation is from loguru")
    return {"message": "Hello World"}
@app.get("/test_loguru")
@logger.catch(reraise=True)
async def test_loguru():
    logger.info("test_loguru")
    a = 1
    b = 0
    c = a / b
    return {"message": f"Hello {c}"}

@app.get("/health")
async def health_check():
    return {"code": "200"}

if __name__ == "__main__":
    uvicorn.run(app, host='0.0.0.0', port=settings.SERVER_PORT)